package com.rftennis.web.servlets;

import java.io.IOException;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.rftennis.dto.CustomerContext;
import com.rftennis.dto.SystemAdminContext;
import com.rftennis.exception.RftennisException;
import com.rftennis.service.CustomerService;
import com.rftennis.service.SystemAdminService;
import com.rftennis.service.impl.CustomerServiceImpl;
import com.rftennis.service.impl.SystemAdminServiceImpl;


public class CustomerLoginServlet extends HttpServlet{


	
@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	try{// 1获取参数
		String account = req.getParameter("account");
		String password = req.getParameter("password");
		
		
		
		// 2调用业务模型，获取业务结果
		CustomerService service = new CustomerServiceImpl();
		//ServletContext sc1=this.getServletContext();
		
		//String cx=sc1.getContextPath();//"/07" 
		
			CustomerContext cc=service.login(account, password);
			// 3视图处理
			// 3.1 成功处理
			HttpSession session =req.getSession(false);
			//说明第一次登录
			if(session==null) {
				
			}else {
				//之前登陆过
				//销毁之前的session
				session.invalidate();
			}
			session=req.getSession();
			session.setAttribute("model", cc);
			String url=req.getContextPath()+"/index.jsp";
			resp.sendRedirect(resp.encodeRedirectURL(url));
			//req.setAttribute("model", sc);
			//req.getRequestDispatcher("/index.jsp").forward(req, resp);
		} catch (RftennisException e) {
			
			 //3.2.1 失败业务逻辑异常
			req.setAttribute("message", e.getMessage());
			
			req.getRequestDispatcher("/login.jsp").forward(req, resp);
		} catch (Exception e) {
			
			e.printStackTrace();
		} 

	}

	
	
}
